﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SADE.MailParser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = String.Join("\r\n", GetEmailAddresses().ToArray());
        }

        private List<string> GetEmailAddresses()
        {
            var input = textBox1.Text;

            var al = new List<string>();

            var mc =
                System.Text.RegularExpressions.Regex.Matches
                    (
                        input,
                        @"[A-Za-z0-9](([_\.\-\+]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})"
                    );

            for (var i = 0; i < mc.Count; i++)
            {
                if (!al.Contains(mc[i].Value))
                    al.Add(mc[i].Value);
            }

            al.Sort();

            return al;
        }
    }
}
